package com.ruoyi.system.service.standard.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.standard.StandardMapper;
import com.ruoyi.system.domain.standard_relevant.Standard;
import com.ruoyi.system.service.standard.IStandardService;

/**
 * 标准相关操作Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-06
 */
@Service
public class StandardServiceImpl implements IStandardService 
{
    @Autowired
    private StandardMapper standardMapper;

    /**
     * 查询标准相关操作
     * 
     * @param id 标准相关操作主键
     * @return 标准相关操作
     */
    @Override
    public Standard selectStandardById(Long id)
    {
        return standardMapper.selectStandardById(id);
    }

    /**
     * 查询标准 By StandardCode
     *
     * @param standardCode 标准号
     * @return result
     */
    @Override
    public Standard selectStandardByCode(String standardCode){
        return standardMapper.selectStandardByCode(standardCode);
    }

    /**
     * 查询标准相关操作列表
     * 
     * @param standard 标准相关操作
     * @return 标准相关操作
     */
    @DataScope(deptAlias = "s_base", userAlias = "s_base")
    @Override
    public List<Standard> selectStandardList(Standard standard)
    {
        return standardMapper.selectStandardList(standard);
    }

    /**
     * 新增标准相关操作
     * 
     * @param standard 标准相关操作
     * @return 结果
     */
    @Override
    public int insertStandard(Standard standard)
    {
        standard.setCreateTime(DateUtils.getNowDate());
        return standardMapper.insertStandard(standard);
    }

    /**
     * 修改标准相关操作
     * 
     * @param standard 标准相关操作
     * @return 结果
     */
    @Override
    public int updateStandard(Standard standard)
    {
        return standardMapper.updateStandard(standard);
    }

    /**
     * 批量删除标准相关操作
     * 
     * @param ids 需要删除的标准相关操作主键
     * @return 结果
     */
    @Override
    public int deleteStandardByIds(Long[] ids)
    {
        return standardMapper.deleteStandardByIds(ids);
    }

    /**
     * 删除标准相关操作信息
     * 
     * @param id 标准相关操作主键
     * @return 结果
     */
    @Override
    public int deleteStandardById(Long id)
    {
        return standardMapper.deleteStandardById(id);
    }

    /**
     *  删除标准 By StandardCode
     *
     *  @param standardCode 标准号
     *  @return 结果
     */
    public int deleteStandardByCode(String standardCode){
        return standardMapper.deleteStandardByCode(standardCode);
    }

    /**
     * 批量删除标准 By StandardCode
     * @param standardCodes 需要删除的标准号集合
     * @return 结果
     */
    public int deleteStandardByCodes(String[] standardCodes){
        return standardMapper.deleteStandardByCodes(standardCodes);
    }


    /**
     * 标准按分类号分类的统计信息
     *
     * @return {标准分类号:数量}
     */
    @Override
    public Map<String, Object> countByClassificationCode(){
        List<Map<String, Object>> results = standardMapper.countByClassificationCode();

        Map<String, Object> countMap = new HashMap<>();
        for (Map<String, Object> result : results) {
            String classificationCode = (String) result.get("classification_code");
            Long count = (Long) result.get("count");
            System.out.println("classificationCode: " + classificationCode + " count: " + count);
            countMap.put(classificationCode, count);
        }
        return countMap;
    }

    /**
     * 查询所有标准号
     */
    @Override
    public List<String> selectAllStandardCode(){
        return standardMapper.selectAllStandardCode();
    }

}
